package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import org.junit.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author: Javy
 * @since: 2021/1/19
 */
@RestController
@RequestMapping("/usermanagement")
public class UserController {

    /**
     * 订阅检查项服务
     */
    @Reference
    private UserService userService;

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        // 调用服务查询
        List<User> list = userService.findAll();
        // 封装到Result再返回
        return new Result(true, MessageConstant.QUERY_USER_SUCCESS,list);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/add")
    // @PreAuthorize("hasAuthority('USER_ADD')")
    public Result add(@RequestBody User user,Integer[] roles){
        // 调用服务添加
        userService.add(user,roles);
        // 返回操作的结果
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    /**
     * 用户的分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        // 调用服务 分页查询
        PageResult<User> pageResult = userService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_USER_SUCCESS,pageResult);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id){
        User user = userService.findById(id);
        return new Result(true, MessageConstant.QUERY_USER_SUCCESS,user);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody User user, Integer[] roles){
        // 调用服务更新
        userService.update(user,roles);
        // 返回操作的结果
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(int id){
        userService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }

    @PostMapping("/resetPassword")
    public Result resetPassword(int id){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = bCryptPasswordEncoder.encode("123456");
        userService.resetPassword(id,password);
        return new Result(true,MessageConstant.RESET_USER_SUCCESS);
    }
    @GetMapping("/findByMenu")
    public Result findByMenu(){
        List<Role> roleList = userService.findByMenu();
        return new Result(true,"查询角色信息成功",roleList);
    }
    @GetMapping("/findByMenuRole")
    public Result findByMenuRole(int id){
        List<Integer> idList = userService.findByMenuRole(id);
        return new Result(true,"查询角色id成功",idList);

    }
}

